﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using libxl;
using System.Reflection;
using System.Collections;
using System.IO;
using Spire.Xls;
using System.Data;
using System.Xml;
using Newtonsoft.Json;
using Excel;

namespace SerializationTool
{
    class Serializer
    {
        public enum StructType
        {
            eDictionary,
            eList,
            ePrivateStruct,
            eNormal,
        }

        private static Serializer _Serializer = null;
        public static Serializer Instance
        {
            get
            {
                if (_Serializer == null)
                    _Serializer = new Serializer();
                return _Serializer;
            }
        }

        public Dictionary<string, List<string>> SheetDic = new Dictionary<string, List<string>>();

        public static void Log(string param)
        {
            //           Console.WriteLine(param);
            Debug.Log(param);
        }
       public void  Serialize<T>(T structObj)
        {
//             Excel.Core. app = new Excel.Application();
//             Excel.Workbook workBook = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//             Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];
// 
//             if (app == null)
//             {
//                 Log("Init error");
//                 return;
//             }
            if(CheckType(structObj.GetType()) == StructType.ePrivateStruct)
            {
                int colIdx = 0;
                DealWithObject(structObj,ref colIdx);
            }
            if(CheckType(structObj.GetType()) == StructType.eList)
            {
                int colIdx = 0;
                DealWithList<object>(structObj,ref colIdx);
            }
        }

        private void WriteExcel(string sheetName, List<string> list,int colCount)
        {

        }

        private void DealWithObject(object oClass,ref int colIdx)
        {
            if(oClass == null)
            {
                Log("data error");
                return;
            }

            foreach (var item in oClass.GetType().GetProperties())
            {
                Log(item.Name);
            }
        }

        private void DealWithList<T>(T list,ref int colIdx) where T : new()
        {
            IList _dataList = (IList)list;
            foreach(var o in _dataList)
            {
                if (CheckType(o.GetType()) == StructType.ePrivateStruct)
                    DealWithObject(o,ref colIdx);
                else if (CheckType(o.GetType()) == StructType.eList)
                {
                    DealWithList<object>(o, ref colIdx);
                }
                else
                {
                    Log(o.GetType().Name);
                }

            }
            colIdx++;
        }

        public static void Write(string path, string param)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(param);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();

            Log("Code generation successful");
        }
        private static bool IsBulitinType(Type type)
        {
            return (type == typeof(object) || Type.GetTypeCode(type) != TypeCode.Object);
        }
        public static StructType CheckType(Type type)
        {
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return StructType.eDictionary;
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                return StructType.eList;
            }
            else if (type.IsClass)
            {
                return StructType.ePrivateStruct;
            }
            else
            {
                return StructType.eNormal;
            }
        }
    }
}
